package common

import (
	"fmt"
	"github.com/axgle/mahonia"
	"github.com/kardianos/service"
	"net"
	"net/http"
	"os/exec"
	"runtime"
	"strings"
	"sync"
)

var (
	// M 安全锁
	M *sync.Mutex
	// Cmd agent进程
	Cmd *exec.Cmd
	// Service daemon服务
	Service service.Service
	// ServerIP 服务IP地址
	ServerIP string
	// AgentStatus agent状态，是否启动中
	AgentStatus bool
	// InstallPath agent安装目录
	InstallPath string
	// Arch 系统位数
	Arch string
	// PublicKey 与Server通讯公钥
	PublicKey string
	// HTTPClient httpclient
	HTTPClient *http.Client
	// Proto 请求协议，测试模式为HTTP
	Proto string
)


func init(){

}

//结束Agent
func KillAgent() error{
	if AgentStatus{
		return Cmd.Process.Kill()
	}
	return nil;
}

// CmdExec 执行系统命令
func CmdExec(cmd string)(string, error){
	var c *exec.Cmd
	var data string
	system := runtime.GOOS
	if system == "windows"{
		argArray := strings.Split("/c "+cmd," ")
		c = exec.Command("cmd",argArray...)
	}else{
		c = exec.Command("/bin/sh","-c",cmd)
	}
	out, err := c.CombinedOutput()
	if err != nil{
		return data, err
	}
	data = string(out)
	if system == "windows"{
		dec := mahonia.NewDecoder("gbk")
		data = dec.ConvertString(data)
	}
	return data,nil
}


//判断值是否存在于指定列表中，like为true则为包含判断
func InArray(list []string,value string, like bool)bool{
	for _, v := range list{
		if like{
			if strings.Contains(value,v){
				return true
			}
		}else{
			if value == v{
				return true
			}
		}
	}
	return false
}

// 获取一个可以绑定的内网IP  本地的IP
func BindAddr()string{
	// 通过连接一个可达的任何一个地址，获取本地的内网的地址
	conn, _ := net.Dial("udp","114.114.114.114:53")
	defer conn.Close()
	localAddr := conn.LocalAddr().String()
	idx := strings.LastIndex(localAddr,":")
	return fmt.Sprintf("%s:65512",localAddr[0:idx])
}